<template>
  <div style="margin:10px">
    <zh-space>
      <zh-card title="">
        <zh-form :model="pageData.queryParams" ref="queryFormRef" label-width="70px" :span="8">
          <zh-form-select
              prop="id"
              :defaultFlag="true"
              label="数据源"
              v-model="pageData.queryParams.id"
              :lazy="true"
              func-code="genDatasource0001"
              :fieldNames="{ label: 'connName', value: 'id' }"
              @change="query"
          ></zh-form-select>
          <zh-form-input
              prop="tableName"
              label="表名"
              v-model="pageData.queryParams.tableName"
          ></zh-form-input>
          <zh-form-query-button  @queryClick="query">
          </zh-form-query-button>
        </zh-form>
      </zh-card>
      <zh-table title="" type="default" ref="queryTable"  size="mini" funcCode="genDatasource0008" :columns="pageData.tableColumns" highlight-current-row>
      </zh-table>
    </zh-space>
   
  </div>
</template>
<script lang="ts" setup name="importForm">
import { ElMessage } from 'element-plus';
// ------------------接收传入参数(组件)-------------------
const emits = defineEmits(['update:btnLoding', 'canner', 'refreshTable']);
// ------------------钩子函数----------------------------
// 初始化
onMounted(() => {
});

// ------------------定义子组件--------------------------
const queryTable = ref();
const queryFormRef = ref();

// ------------------变量区------------------------------
const pageData = reactive({
  advanced: false,
  tableColumns: [
    { type: 'checkbox', width: 60, fixed: 'left', align: 'center'},
    {
      type: 'seq',
      width: 60,
      fixed: 'left',
      align: 'center',
      title: '序号',
    },
    
    {
      field: 'tableName',
      title: '表名',
      visible: true,
      align: 'center',
      minWidth: 150,
    },
    {
      field: 'tableComment',
      title: '表说明',
      visible: true,
      align: 'center',
      minWidth: 300,
    }
  ],
  queryParams: {} as any,
  dictData: {} as any,
  title: '',
  detailVisible: false,
  showFlag: false,
  selectRow: {} as any,
  saveLoading: false,
});
// ------------------方法区------------------------------
//刷新
const query = () => {
  if(!pageData.queryParams.id){
    ElMessage.warning('请先选择数据源');
    return;
  }
  queryTable.value.query(pageData.queryParams);
};
async function save() {
  if (!pageData.queryParams.id) {
		ElMessage.warning('请选择数据源')
		return
	}
  let tableList=queryTable.value.getAllCheckboxRecords();
  if (tableList.length === 0) {
		ElMessage.warning('请选择记录')
		return
	}
  if (tableList.length > 5) {
		ElMessage.warning('不能选择超过5条记录')
		return
	}
  let tableNameList=[];
  let datasourceId=pageData.queryParams.id
  tableList.forEach((item)=>{
    tableNameList.push(item.tableName)
    datasourceId=item.datasourceId
  })
  let params={datasourceId:datasourceId,tableNameList:tableNameList}
  $api.common.send
      .post('genTable0008', params)
      .then(() => {
        ElMessage.success('导入成功');
        emits('update:btnLoding', false);
        emits('canner', (pageData.detailVisible = false));
        emits('refreshTable');
      })
      .catch(() => {
        emits('update:btnLoding', false);
        emits('canner', (pageData.detailVisible = true));
      });
  
}
//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ save });
</script>
<style lang="scss" scoped></style>
